package com.mxpio.erp.plm.controller;

import com.mxpio.erp.common.plm.entity.ProjectDeliverable;
import com.mxpio.erp.common.plm.entity.ProjectDeliverableTemplate;
import com.mxpio.erp.common.plm.service.ProjectDeliverableTemplateService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "ProjectDeliverableTemplateController", description = "项目-交付物模板")
@RestController
@RequestMapping("/erp/plm/pro/deliverables/template/")
public class ProjectDeliverableTemplateController {
    @Autowired
    private ProjectDeliverableTemplateService projectDeliverableTemplateService;

    @GetMapping("list")
    @Operation(summary = "项目交付物模板列表", description = "项目交付物模板列表（无分页）", method = "GET")
    public Result<List<ProjectDeliverableTemplate>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<ProjectDeliverableTemplate> list = projectDeliverableTemplateService.list(ProjectDeliverableTemplate.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "项目交付物模板列表", description = "项目交付物模板列表（分页）", method = "GET")
    public Result<Page<ProjectDeliverableTemplate>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<ProjectDeliverableTemplate> pageResult = projectDeliverableTemplateService.listPage(ProjectDeliverableTemplate.class, page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("list/{docNO}")
    @Operation(summary = "根据项目模板交付物编码-获取交付物信息", description = "根据项目模板交付物编码-获取交付物信息", method = "GET")
    public Result<ProjectDeliverableTemplate> getById(@PathVariable(name = "docNO", required = true) String docNO) {
        ProjectDeliverableTemplate deliverables = projectDeliverableTemplateService.getById(ProjectDeliverableTemplate.class, docNO);
        return Result.OK(deliverables);
    }

//    @RequestMapping("add")
//    @Operation(summary = "保存项目模板交付物信息", description = "保存项目模板交付物信息")
//    public Result<ProjectDeliverableTemplate> add(@RequestBody ProjectDeliverableTemplate deliverables) {
//        projectDeliverableTemplateService.save(deliverables);
//        return Result.OK(deliverables);
//    }

    @PutMapping("edit")
    @Operation(summary = "编辑项目模板信息", description = "编辑项目模板信息", method = "PUT")
    public Result<ProjectDeliverableTemplate> edit(@RequestBody ProjectDeliverableTemplate deliverables) {
        projectDeliverableTemplateService.update(deliverables);
        return Result.OK(deliverables);
    }


    @DeleteMapping("/delete/{docNO}")
    @Operation(summary = "通过docNO删除", description = "通过docNO删除", method = "DELETE")
    public Result<ProjectDeliverableTemplate> delete(@PathVariable(name="docNO",required=true) String docNO) {
        projectDeliverableTemplateService.delete( ProjectDeliverableTemplate.class,docNO);
        return Result.OK();
    }

    @GetMapping("list/pro/{templateCode}")
    @Operation(summary = "根据项目模板编码获取项目模板交付物", description = "根据项目模板编码获取项目模板交付物", method = "GET")
    public Result<List<ProjectDeliverableTemplate>> getByList(@PathVariable(name = "templateCode", required = true) String templateCode) {
        Criteria criteria = Criteria.create().addCriterion("templateCode", Operator.EQ, templateCode);
        List<ProjectDeliverableTemplate> list = projectDeliverableTemplateService.list(ProjectDeliverableTemplate.class, criteria);
        return Result.OK(list);
    }
}
